home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: To malloc (new) or not to malloc? Clarification. Please ignore previous post.
- Date: Tue, 09 Jan 96 19:12:59 GMT
- Organization: none
- Message-ID: <821214779snz@genesis.demon.co.uk>
- References: <4ctvk3$ort@maverick.tad.eds.com> <4cua5t$dm@maverick.tad.eds.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4cua5t$dm@maverick.tad.eds.com>
- fignet05.darrins@eds.com "Darrin Smith" writes:
-
- >I know that I should know the answer to this (I'm sure one of my past
- >instructors made it clear while I was sleep.... I mean sitting in class),
- >but I can't remember so...
- >
- >Why is it that you can do something like the following:
- >
- > char *x;
-
- This creates an object with type pointer to char and an identifier x which
- can be used to access it. If the declaration is ni a function the pointer is
- uninitialised so it is not legal to use its value. When you define a pointer
- only space for the pointer itself is allocated, never anything for the pointer
- to point to.
-
- > x="Some really long string with no particular meaning";
-
- The string literal defines an object that is a static array of char, causes
- space to be allocated for that object at program startup (like any other
- static object). The array is initialised to the text of the string. The
- address of the first element of the static array is assigned to x which then
- has a well-defined value.
-
- >and have no problems, but can't (safely) do something like this:
- >
- > struct st1{char one[10];
- > char two[20];
- > char three[10];
- > };
- >
- > st1 *sptr; //or struct st1 *sptr in C instead of C++
-
- Since you are posting to comp.lang.c, stick to C.
-
- struct st1 *sptr; creates an object with type struct st2 * which is, again,
- an uninitilised pointer if this is in a function.
-
- > fread(sptr,sizeof(st1),1,infile);
-
- fread requires you to pass a pointer to a buffer where it can store the
- data read. However you are passing the uninitialised value of sptr which
- is illegal.
-
- >This caused a program I was trying to debug to crash. When I allocated
- >memory for sptr (I used sptr=new st1; but I suppose malloc would have done
- >just as well) it worked fine.
-
- malloc would have worked much better since new isn't defined in C!
-
- By doing this you have set sptr to point to a valid buffer. You can therefore
- pass that pointer value to fread.
-
- >What is going on here? Why isn't memory set aside for st1 *sptr just as
- >it is for char *x?
-
- In both cases memory is allocated for the pointer itself. In neither case
- is any other memory allocated - the pointers don't point to anything
- until you assign a valid pointer value to them. The 2 pointr are behaving in
- exactly the same way, the only difference being the method used to create
- an object for them to point to.
-
- > Before I did the new (or malloc) it seemed as though
- >my program was getting written over!
- >
- >Why?
-
- The uninitialised pointer could have held any value (or no meaningful value) -
- it could have pointed right into your program hence causing fread to overwrite
- it.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-